package com.gzhryc.shared_device.oem.h5.mng.controllers.device;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.data.dto.SelectResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.shared_device.base.enums.ENetworkType;
import com.gzhryc.shared_device.common.device.dao.db.DeviceType;
import com.gzhryc.shared_device.common.device.dao.db.DeviceTypeFunc;
import com.gzhryc.shared_device.common.device.services.DeviceTypeFuncService;
import com.gzhryc.shared_device.common.device.services.DeviceTypeService;
import com.gzhryc.shared_device.common.device.services.dto.DeviceTypeSearch;
import com.gzhryc.shared_device.oem.code.device.DeviceEventListener;
import com.gzhryc.shared_device.oem.code.device.EDeviceFunCode;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class DeviceTypeController {

    @Route(value = { "/device/type/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("networkTypeList", ENetworkType.values());
        return "/device/type/list.html";
    }

    @Route(value = "/device/type/list.data")
    public ListResult<DeviceType> listData(HttpServletRequest request) {
        ListResult<DeviceType> result = new ListResult<>(DtoConstants.NULL_DATA,"暂无数据");

        DeviceTypeSearch search = WebUtils.getParameter(request, DeviceTypeSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(DeviceTypeService.self().countBySearch(search));
        List<DeviceType> deviceTypeList = DeviceTypeService.self().findBySearch(search, pageNo, pageLimit);
        if(deviceTypeList != null && deviceTypeList.size() > 0) {
        	String resDomain = WebConstants.getResDomain(request);
            List<String> deviceTypeIds = new ArrayList<>();
            for(DeviceType deviceType: deviceTypeList){
                if(StringTools.isNotBlank(deviceType.getThumbnailImg())){
                    deviceType.setThumbnailImg(resDomain + deviceType.getThumbnailImg());
                }
                deviceTypeIds.add(deviceType.getId());
            }

            List<DeviceTypeFunc> deviceTypeFuncList = DeviceTypeFuncService.self().findByTypeIds(deviceTypeIds);
            if(deviceTypeFuncList != null && deviceTypeFuncList.size() > 0){
                for(DeviceType deviceType: deviceTypeList){
                    for(DeviceTypeFunc deviceTypeFunc : deviceTypeFuncList){
                        if(deviceTypeFunc.getTypeId().equals(deviceType.getId())){
                            if(StringTools.isNotBlank(deviceType.getFuncCodeNames())){
                                deviceType.setFuncCodeNames(deviceType.getFuncCodeNames() + "," + EDeviceFunCode.note(deviceTypeFunc.getCode()));
                            }else {
                                deviceType.setFuncCodeNames(EDeviceFunCode.note(deviceTypeFunc.getCode()));
                            }
                        }
                    }
                }
            }

            result.getBody().setData(deviceTypeList);
        }

        if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
            result.setCode(DtoConstants.SUCCESS, null);
        }

        return result;
    }

    @Route(value = { "/device/type/add.html" })
    public String add(HttpServletRequest request) {
        request.setAttribute("networkTypeList", ENetworkType.values());
        boolean localRes = PropertyTools.get().getBoolean("localRes");
    	if(localRes) {
    	     return "/device/type/edit2.html";
    	}
        return "/device/type/edit.html";
    }

    @Route(value = { "/device/type/add.do" })
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增类型失败");
        DeviceType deviceType = WebUtils.getParameter(request, DeviceType.class);
        try {
            deviceType.setId(deviceType.getId().toUpperCase());	//必须大写
            if(DeviceTypeService.self().add(deviceType)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/device/type/info.html" })
    public String info(HttpServletRequest request) {
        String id = WebUtils.getString(request, "id");
        DeviceType deviceType = DeviceTypeService.self().getById(id);
        WebUtils.setAttribute(request, deviceType);
        request.setAttribute("networkTypeList", ENetworkType.values());
        return "/device/type/info.html";
    }

    @Route(value = { "/device/type/update.html" })
    public String update(HttpServletRequest request) {
        String id = WebUtils.getString(request, "id");
        DeviceType deviceType = DeviceTypeService.self().getById(id);
        WebUtils.setAttribute(request, deviceType);
        request.setAttribute("networkTypeList", ENetworkType.values());
        boolean localRes = PropertyTools.get().getBoolean("localRes");
    	if(localRes) {
    	     return "/device/type/edit2.html";
    	}
        return "/device/type/edit.html";
    }

    @Route(value = { "/device/type/update.do" })
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改设备类型失败");
        DeviceType deviceType = WebUtils.getParameter(request, DeviceType.class);
        try {
            if (DeviceTypeService.self().updateNotNull(deviceType,new DeviceEventListener())) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/device/type/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除设备类型失败");
        String id = WebUtils.getString(request, "id");
        if(StringTools.isNotBlank(id)) {
            try {
                if(DeviceTypeService.self().deleteById(id)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route("/device/type/select.data")
    public SelectResult selectData(HttpServletRequest request) {
        SelectResult result = new SelectResult(DtoConstants.SUCCESS);
        String term = request.getParameter("term");
        Integer page = WebUtils.getInteger(request, "page", 1);

        DeviceTypeSearch search = new DeviceTypeSearch();
        search.setName(term);
        Long total = 0L;
        List<DeviceType> list = DeviceTypeService.self().findBySearch(search, page, WebConstants.PAGE_LIMIT);
        if (list != null) {
            for (DeviceType deviceType : list) {
                SelectResult.Item item = new SelectResult.Item();
                item.setId(deviceType.getId());
                item.setText(deviceType.getName());
                result.getBody().getData().add(item);
            }
            total += list.size();
        }
        result.getBody().setPage(page);
        result.getBody().setLimit(WebConstants.PAGE_LIMIT);
        result.getBody().setTotal(total);
        return result;
    }
}
